package com.ztgf.order.service.purchase;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.models.dto.CommodityInfoDTO;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.order.common.config.YmlConfig;
import com.ztgf.order.mapper.purchase.PurchaseCommodityBargainMapper;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityBargainSaveBO;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityBargainDTO;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityDTO;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityBargainService;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityService;
import com.ztgf.order.models.vo.purchase.PurchaseCommodityVO;
import com.ztgf.shopping.models.dto.shop.ShopShoppingCartDTO;
import com.ztgf.shopping.models.dto.shop.ShopSysInvoiceDTO;
import com.ztgf.shopping.models.service.shop.IShopShoppingCartService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店铺报价表控制器
 *
 * @author mxli
 * @Date 2021-05-17 10:16:52
 */
@DubboService
@Lazy
public class PurchaseCommodityBargainServiceImpl extends ServiceImpl<PurchaseCommodityBargainMapper, PurchaseCommodityBargainDTO> implements IPurchaseCommodityBargainService{

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IShopShoppingCartService shopShoppingCartService;

    @Autowired
    private IPurchaseCommodityService purchaseCommodityService;

    @DubboReference
    private ICommodityInfoService commodityInfoService;


    @Override
    public boolean add(PurchaseCommodityBargainSaveBO saveBO) {
        PurchaseCommodityBargainDTO dto = new PurchaseCommodityBargainDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public boolean addToCart(Long id,Long shopId) {
        //查询对应询盘报价数据
        PurchaseCommodityBargainDTO dto = this.selectById(id);
        if(null == dto){
            return false;
        }

        //获取询盘商品表数据
        PurchaseCommodityVO purchaseCommodityVO = purchaseCommodityService.getPurchaseCommodity(dto.getPurchaseId());
        PurchaseCommodityDTO purchaseCommodityDTO = new PurchaseCommodityDTO();
        BeanUtils.copyProperties(purchaseCommodityVO, purchaseCommodityDTO);

        CommodityInfoDTO commodityInfoDTO = null;
        //判断上品是上架商品还是虚拟商品
//        if(dto.getCommodityPublishType() == 0){
//           //获取商品所属店铺id
//            commodityInfoDTO = iCommodityInfoService.selectById(dto.getCommodityId());
//        }
        //判断是否购物车已有
        Wrapper<ShopShoppingCartDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId).eq("purchase_commodity_bargain_id", dto.getId())
                .eq("price",dto.getPrice());
        ShopShoppingCartDTO shopShoppingCartDTO = shopShoppingCartService.selectOne(wrapper);
        ShopShoppingCartDTO shopdto = new ShopShoppingCartDTO();
        if (shopShoppingCartDTO != null) {
            //购物车已经存在
            //shopdto.setId(shopShoppingCartDTO.getId());
            //shopdto.setNumber(shopShoppingCartDTO.getNumber() + dto.getNum());
            //return shopShoppingCartService.updateById(shopdto);
            return true;
        } else {
            BeanUtils.copyProperties(dto, shopdto);
            Long pkid = ymlConfig.getPkId();
            shopdto.setId(pkid);
            shopdto.setType(1);
            shopdto.setCommodityShopId(dto.getShopId());//商品所属的店铺id
            shopdto.setNumber(dto.getNum());
            shopdto.setCommodityId(purchaseCommodityVO.getId());//将询盘商品表id做为商品id
            shopdto.setShopId(shopId);
            shopdto.setPurchaseCommodityBargainId(dto.getId());
            shopdto.setSpecificationModel(purchaseCommodityDTO.getSpecificationModel());
            return shopShoppingCartService.insert(shopdto);
        }
    }

    /**
     * 同步固定申请平台砍价固定费率29.8
     * @return
     */
    @Override
    public List<ShopSysInvoiceDTO> synchronizationShopSysInvoice() {
        return baseMapper.synchronizationShopSysInvoice();
    }
}
